package net.guerlab.smart.wx.api.autoconfig;

import lombok.AllArgsConstructor;
import net.guerlab.smart.wx.api.WxUserLoginLogApi;
import net.guerlab.smart.wx.api.feign.FeignWxUserLoginLogApi;
import net.guerlab.smart.wx.core.domain.WxUserLoginLogDTO;
import net.guerlab.smart.wx.core.searchparams.WxUserLoginLogSearchParams;
import net.guerlab.spring.searchparams.SearchParamsUtils;
import net.guerlab.web.result.ListObject;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * @author guer
 */
@Configuration
@AutoConfigureAfter(WxUserLoginLogApiLocalServiceAutoConfigure.class)
public class WxUserLoginLogApiFeignAutoConfigure {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Bean
    @ConditionalOnMissingBean(WxUserLoginLogApi.class)
    public WxUserLoginLogApi wxUserLoginLogApiFeignWrapper(FeignWxUserLoginLogApi api) {
        return new WxUserLoginLogApiFeignWrapper(api);
    }

    @AllArgsConstructor
    private static class WxUserLoginLogApiFeignWrapper implements WxUserLoginLogApi {

        private FeignWxUserLoginLogApi api;

        @Override
        public ListObject<WxUserLoginLogDTO> findList(WxUserLoginLogSearchParams searchParams) {
            Map<String, Object> params = new HashMap<>(8);
            SearchParamsUtils.handler(searchParams, params);
            return Optional.ofNullable(api.findList(params).getData()).orElse(ListObject.empty());
        }

        @Override
        public List<WxUserLoginLogDTO> findAll(WxUserLoginLogSearchParams searchParams) {
            Map<String, Object> params = new HashMap<>(8);
            SearchParamsUtils.handler(searchParams, params);
            return Optional.ofNullable(api.findAll(params).getData()).orElse(Collections.emptyList());
        }
    }

}
